package yxy.game.pm2.command.passbarrier;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.passbarrier.PassbarrierHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;


/****************************************
 * @name CMD_PassBarrierRecord
 * @description 斩关夺隘主挑战记录
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierRecord, classtype = PassBarrierMessage.PassBarrierRecordRequest.class)
public class CMD_PassBarrierRecord extends Command<PassBarrierMessage.PassBarrierRecordRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierRecordRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        PassBarrierMessage.PassBarrierRecordResponse.Builder response = PassBarrierMessage.PassBarrierRecordResponse.newBuilder();
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }
            PassbarrierHandle handle = new PassbarrierHandle();

            handle.record(playerCode, response);

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierRecordResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
